﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ClearingGraphAutomaton.UserControls
{
    /// <summary>
    /// NumericUpDown for ToolStrip.
    /// </summary>
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
    public class ToolStripNumericUpDown : ToolStripControlHost
    {
        /// <summary>
        /// Default constuctor.
        /// </summary>
        public ToolStripNumericUpDown() :
            base(new NumericUpDown())
        {

        }

        /// <summary>
        /// NumericUpDown control.
        /// </summary>
        public NumericUpDown NumericUpDownControl
        {
            get
            {
                return this.Control as NumericUpDown;
            }
        }

        /// <summary>
        /// Gets or sets the number of decimal places to display in the spin box (also
        /// known as an up-down control).
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// The value assigned is less than 0.-or- 
        /// The value assigned is greater than 99.
        /// </exception>
        public int DecimalPlaces
        {
            get
            {
                return this.NumericUpDownControl.DecimalPlaces;
            }
            set
            {
                this.NumericUpDownControl.DecimalPlaces = value;
            }
        }

        /// <summary>
        /// Gets or sets the value to increment or decrement the spin box (also known
        /// as an up-down control) when the up or down buttons are clicked.
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// The assigned value is not greater than or equal to zero.
        /// </exception>
        public decimal Increment
        {
            get
            {
                return this.NumericUpDownControl.Increment;
            }
            set
            {
                this.NumericUpDownControl.Increment = value;
            }
        }

        /// <summary>
        /// Gets or sets the maximum value for the spin box (also known as an up-down control).
        /// </summary>
        public decimal Maximum
        {
            get
            {
                return this.NumericUpDownControl.Maximum;
            }
            set
            {
                this.NumericUpDownControl.Maximum = value;
            }
        }

        /// <summary>
        /// Gets or sets the minimum allowed value for the spin box (also known as an up-down control).
        /// </summary>
        public decimal Minimum
        {
            get
            {
                return this.NumericUpDownControl.Minimum;
            }
            set
            {
                this.NumericUpDownControl.Minimum = value;
            }
        }

        /// <summary>
        /// Gets or sets the value assigned to the spin box (also known as an up-down control).
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// The assigned value is less than the NumericUpDownControl.Minimum property value.-or- 
        /// The assigned value is greater than the NumericUpDownControl.Maximum property value.
        /// </exception>
        public decimal Value
        {
            get
            {
                return this.NumericUpDownControl.Value;
            }
            set
            {
                this.NumericUpDownControl.Value = value;
            }
        }

        protected override void OnSubscribeControlEvents(Control c)
        {
            // Call the base so the base events are connected.
            base.OnSubscribeControlEvents(c);

            // Cast the control to a NumericUpDown control.
            NumericUpDown mumControl = (NumericUpDown)c;

            // Add the event.
            mumControl.ValueChanged += new EventHandler(OnValueChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control c)
        {
            // Call the base method so the basic events are unsubscribed.
            base.OnUnsubscribeControlEvents(c);

            // Cast the control to a NumericUpDown control.
            NumericUpDown mumControl = (NumericUpDown)c;

            // Remove the event.
            mumControl.ValueChanged -= new EventHandler(OnValueChanged);
        }

        /// <summary>
        /// Occurs when the System.Windows.Forms.NumericUpDown.Value property has been
        /// changed in some way.
        /// </summary>
        public event EventHandler ValueChanged;

        private void OnValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, e);
            }
        }
    }
}
